package com.centling.smartSealForPhone.activity

import `in`.srain.cube.views.ptr.PtrDefaultHandler
import `in`.srain.cube.views.ptr.PtrFrameLayout
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.adapter.ReviewListAdapter
import com.centling.smartSealForPhone.bean.ReviewListBean
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_review.*
import java.util.*

class ReviewActivity : TitleBarActivity() {

    private val dataList = ArrayList<ReviewListBean.RowsBean>()

    private var pageIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_review)

        setTitleBarText("复审失败")

        initData()

        sendRequest(true)
    }

    private fun initData() {
        rv_review_list.layoutManager = GridLayoutManager(mContext, 1)
        val reviewListAdapter = ReviewListAdapter(this, dataList)
        rv_review_list.adapter = reviewListAdapter
        reviewListAdapter.setOnItemClickListener { _, pos ->
            val intent = Intent(mContext, ReviewDetailsActivity::class.java)
            intent.putExtra("sealApplyId", dataList[pos].sealApplyId)
            if (dataList[pos].reviewResultType != null) {
                intent.putExtra("state", dataList[pos].reviewResultType == "ILLEGAL")
            }
            startActivity(intent)
        }
        reviewListAdapter.setOnButtonClickListener { _, pos ->
            val intent = Intent(mContext, ReviewUploadActivity::class.java)
            intent.putExtra("sealApplyId", dataList[pos].sealApplyId)
            startActivity(intent)
        }

        ptr_review_list.setPtrHandler(object : PtrDefaultHandler() {
            override fun onRefreshBegin(ptrFrameLayout: PtrFrameLayout) {
                sendRequest(true)
            }
        })

        rv_review_list.setLoadDataListener { sendRequest(false) }
    }

    private fun sendRequest(isRefresh: Boolean) {
        if (isRefresh) {
            pageIndex = 0
        }
        getEnqueue(String.format(Locale.CHINA, UrlConstants.REVIEW_LIST_NEW, pageIndex), object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String) {
                ptr_review_list.refreshComplete()
                if (isRefresh) {
                    dataList.clear()
                    rv_review_list.setLoadDataListener { sendRequest(false) }
                }
                val bean = Gson().fromJson(json, ReviewListBean::class.java)
                if (bean.rows != null) {
                    dataList.addAll(bean.rows)
                    rv_review_list.adapter.notifyDataSetChanged()
                    if (bean != null && bean.rows.size < 20) {
                        rv_review_list.loadMoreComplete(true)
                    } else {
                        rv_review_list.loadMoreComplete(false)
                    }
                } else {
                    rv_review_list.loadMoreComplete(true)
                }
                pageIndex++
            }

            override fun onFailed(reason: String) {
                ShowToast.shortTime(reason)
                ptr_review_list.refreshComplete()
                rv_review_list.loadMoreComplete(false)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        ptr_review_list.post { ptr_review_list.autoRefresh() }
    }
}
